<?php

namespace App\Http\Controllers\OpenShop\SiteLocation;

use App\Http\Controllers\BaseController;
use App\Service\SiteLocation\WhiteListService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class WhiteListController extends BaseController
{
    /**
     * 白名单列表
     */
    public function list(Request $request): JsonResponse
    {
        $userId    = (int)$request->get('userId', 0);
        $powerType = (string)$request->get('powerType', '');
        $pageSize  = (int)$request->get('pageSize', 10);
        $pageNum   = (int)$request->get('pageNum', 1);

        return $this->success(WhiteListService::list($userId, $powerType, $pageSize, $pageNum));
    }


    /**
     * 添加&修改
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function save(Request $request): JsonResponse
    {
        $this->validate($request, [
            'id'       => 'int',
            'userId'   => 'int|required',
            'appPower' => 'array',
            'pcPower'  => 'array',
        ]);

        $rmsUserId = getRmsUserId(); // 当前用户
        $id        = (int)$request->post('id', 0);
        $userId    = (int)$request->post('userId', 0);
        $appPower  = (array)$request->post('appPower', []);
        $pcPower   = (array)$request->post('pcPower', []);

        WhiteListService::save($id, $userId, $appPower, $pcPower, $rmsUserId);

        return $this->success();
    }


    /**
     * 添加&修改
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function updateStatus(Request $request): JsonResponse
    {
        $this->validate($request, [
            'id'       => 'int|required',
            'status'   => 'int|required',
        ]);

        $rmsUserId = getRmsUserId(); // 当前用户
        $id        = (int)$request->post('id', 0);
        $status    = (int)$request->post('status', 0);

        WhiteListService::updateStatus($id, $status, $rmsUserId);

        return $this->success();
    }


}